package kubernetescli

import (
	"github.com/Kindling-project/chaos-backend/api"
	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
	"k8s.io/apimachinery/pkg/runtime/serializer"
	"k8s.io/client-go/kubernetes/scheme"
	"k8s.io/client-go/rest"
)

var crdConfig rest.Config
var client *rest.RESTClient

var NetworkChaosCli networkchaos = networkchaos{}
var StressChaosCli stresschaos = stresschaos{}
var JVMChaosCli jvmchaos = jvmchaos{}
var IOChaosCli iochaos = iochaos{}
var HTTPChaosCli httpchaos = httpchaos{}

func SetupConfig(cfg *rest.Config) error {
	v1alpha1.AddToScheme(scheme.Scheme)

	crdConfig = *cfg
	crdConfig.ContentConfig.GroupVersion = &v1alpha1.GroupVersion
	crdConfig.APIPath = "/apis"
	crdConfig.NegotiatedSerializer = serializer.NewCodecFactory(scheme.Scheme)
	crdConfig.UserAgent = rest.DefaultKubernetesUserAgent()

	var err error
	client, err = rest.UnversionedRESTClientFor(&crdConfig)

	return err
}

type ChaosCli interface {
	List() (interface{}, error)
	Create(*api.Chaos) (interface{}, error)
	DeleteAll() (interface{}, error)
	ToChaosVO(obj *interface{}) []api.Chaos
}
